package com.ml.mall.utils;

import java.math.BigDecimal;

/**
 * Created by kyyang on 2021/8/9 22:48
 */
public class OrderPriceUtils {


    /**
     * @description: type = 1 5分钱算法  type = 2 10分钱算法
     * 5分钱算法
     * 1.01~1.05 ==1.15  1.06~1.10 ==1.10
     * 10分钱算法
     * 1.01~1.10 ==1.10
     * @author kyyang
     * @date 2021/8/9 22:53
     * @version 1.0
     */
    public static BigDecimal getOrderPriceByCentType(int type,BigDecimal total){
       if(total==null||total.compareTo(BigDecimal.ZERO)<1){
            return BigDecimal.ZERO;
       }
        total = total.setScale(2,BigDecimal.ROUND_HALF_DOWN);
       //浮点数精度有问题；需要拆分出来两个整数
        String[] split = total.toString().split("\\.");
//        System.out.println(split[0]+"===="+split[1]);
        int base = Integer.valueOf(split[1]);
        //获取分位数
        int p = base%10;  //第二位
        int j = base/10;  //第一位
//        System.out.println("p==" +p);
//        System.out.println("j=="+j);

        //5分钱算法
        if(type==1){
            if(1<=p&&p<=5){
                p=5;
            }
            if(6<=p&&p<=9){
                p=0;
                j+=1;
            }
        }

        //10分钱算法
        if(type==2){
            if(1<=p&&p<=9){
                p=0;
                j+=1;
            }
        }
        //拼接字符
        String str = split[0]+"."+j+p;
        BigDecimal countNum = new BigDecimal(str);
        return countNum;
    }


    public static void main(String[] args) {
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(32.23)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.312)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.42)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.53)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.64)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.77)));
//
//        System.out.println("##################");
//        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.01)));
//        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.04)));
//        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.05)));
//        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.06)));
//        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.09)));
//        System.out.println(getsysOrderNewOrderPriceByCentType(2,BigDecimal.valueOf(1.10)));
    }


}
